package view.subscriberpanels;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.HashSet;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import view.ViewManager;
import view.util.ViewUtil;
import data.DataManager;

/**
 * The left panel. Used to select from the visible items to see more details. Details aren't
 * displayed yet though...
 */
public class SelectionPane extends VisSubscriber {
	
	private JList runnerList;
	private String selectedRunner;
	private JPanel runnerInfoPanel;
	
	private void setupView() {
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		JLabel titleLabel = (JLabel) ViewUtil.createLabelWithAlignment("Selection", CENTER_ALIGNMENT);
		titleLabel.setFont(new Font(this.getFont().getName(), Font.BOLD, this.getFont().getSize()+2));
		this.add(titleLabel);
		this.add(Box.createRigidArea(new Dimension(0,20)));
		
	}
	
	public void showOverallView() {
		this.removeAll();
		setupView();
		this.add(ViewUtil.createLabelWithAlignment("Runner Selection", LEFT_ALIGNMENT));
		this.add(Box.createRigidArea(new Dimension(0,10)));
		
		HashSet<Object> runners = DataManager.getInstance().getUniqueColumnEntries("Name",
				ViewManager.getCurrentVis().getTupleSet());
		
		final JLabel runnerNameLabel = new JLabel();
		runnerNameLabel.setMinimumSize(new Dimension(180, 50));
		
		runnerList = new JList(runners.toArray());
		runnerList.setSelectionModel(new DefaultListSelectionModel(){


            @Override
            public void setSelectionInterval(int index0, int index1) {
                if (index0==index1) {
                    if (isSelectedIndex(index0)) {
                        removeSelectionInterval(index0, index0);
                        return;
                    }
                }
                super.setSelectionInterval(index0, index1);
            }

            @Override
            public void addSelectionInterval(int index0, int index1) {
                if (index0==index1) {
                    if (isSelectedIndex(index0)) {
                        removeSelectionInterval(index0, index0);
                        return;
                    }
                super.addSelectionInterval(index0, index1);
                }
            }

        });

		runnerList.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				
				if (e.getValueIsAdjusting()) {
					updateRunnerInfo();
					runnerNameLabel.setText((String)runnerList.getSelectedValue());
					Object[] selected = runnerList.getSelectedValues();
					ViewManager.getCurrentVis().clearSelectedNames();
					String query = "";
					for (int i = 0; i < selected.length; i++){
						if (i == 0) {
							query = "Name = '" + selected[0] + "'";
						} else {
							query += " OR Name = '" + selected[i] + "'";
						}
						ViewManager.getCurrentVis().addSelectedName((String)selected[i]);
					}
					ViewManager.updateVis();
				}
			}
		});
		JScrollPane runnerScrollPane = new JScrollPane(runnerList);
		runnerScrollPane.setPreferredSize(new Dimension(180, 300));
		runnerScrollPane.setMaximumSize(new Dimension(180, 300));
		runnerScrollPane.setMinimumSize(new Dimension(180, 300));
		this.add(runnerScrollPane);
		
		this.add(new JSeparator(JSeparator.HORIZONTAL));
		runnerInfoPanel = new JPanel();
		runnerInfoPanel.setMinimumSize(new Dimension(180, 300));
		this.add(runnerInfoPanel);
		
		

		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());

	}
	
	private void updateRunnerInfo() {
		runnerInfoPanel.removeAll();
		String[] runnerData = DataManager.getInstance().getRunnerData((String)runnerList.getSelectedValue());
		runnerInfoPanel.setLayout(new GridLayout(5, 2));
		runnerInfoPanel.add(new JLabel("Name"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[0]));
		runnerInfoPanel.add(new JLabel("School"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[1]));
		runnerInfoPanel.add(new JLabel("Year"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[2]));
		runnerInfoPanel.add(new JLabel("Avg Time"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[3]));
		runnerInfoPanel.add(new JLabel("Avg Place"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[4]));
		this.repaint();
		this.paintAll(this.getGraphics());
	}
	
	private void updateRunnerInfo(final String runner) {
		runnerInfoPanel.removeAll();
		String[] runnerData = DataManager.getInstance().getRunnerData(runner);
		runnerInfoPanel.setLayout(new GridLayout(5, 2));
		runnerInfoPanel.add(new JLabel("Name"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[0]));
		runnerInfoPanel.add(new JLabel("School"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[1]));
		runnerInfoPanel.add(new JLabel("Year"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[2]));
		runnerInfoPanel.add(new JLabel("Avg Time"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[3]));
		runnerInfoPanel.add(new JLabel("Avg Place"));
		runnerInfoPanel.add(new JLabel(runnerData == null ? "" : runnerData[4]));
		this.repaint();
		this.paintAll(this.getGraphics());
	}
	
	public void selectAllRunners() {
		runnerList.setSelectionInterval(0, runnerList.getModel().getSize() -1);
		reapplySelection();
	}
	public void selectRunner(final String name) {
		updateRunnerInfo(name);
		Object[] selected = {name};
		ViewManager.getCurrentVis().clearSelectedNames();
		String query = "";
		for (int i = 0; i < selected.length; i++){
			if (i == 0) {
				query = "Name = '" + selected[0] + "'";
			} else {
				query += " OR Name = '" + selected[i] + "'";
			}
			ViewManager.getCurrentVis().addSelectedName((String)selected[i]);
		}
		ViewManager.updateVis();
	}
	
	private void reapplySelection() {
		Object[] selected = runnerList.getSelectedValues();
		ViewManager.getCurrentVis().clearSelectedNames();
//		selectedRunner = (String) selected[0];
		String query = "";
		for (int i = 0; i < selected.length; i++){
			if (i == 0) {
				query = "Name = '" + selected[0] + "'";
			} else {
				query += " OR Name = '" + selected[i] + "'";
			}
			ViewManager.getCurrentVis().addSelectedName((String)selected[i]);
		}
		ViewManager.updateVis();
	}
	
	@Override
	void showDateZoomView() {
		reapplySelection();
	}
	@Override
	void showMeetZoomView() {
		reapplySelection();
	}
	@Override
	void showTableView() {
		reapplySelection();
	}
	@Override
	void updateView() {
		this.removeAll();
		showOverallView();
	}
}

